package grape.sequence.rest.config.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.sequence.service.generator.api.ISequenceGenerator;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.sequence.rest.config.form.ConfigCreateForm;
import grape.sequence.rest.config.form.ConfigUpdateForm;
import grape.sequence.rest.config.form.ConfigListPageForm;
import grape.sequence.rest.config.vo.ConfigVo;
import grape.sequence.rest.config.mapper.ConfigWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.sequence.service.config.po.Config;
import grape.sequence.service.config.api.IConfigService;
import java.util.List;
/**
 * <p>
 * 序列生成配置 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-08-20
 */
@RestController
@RequestMapping("/sequence/config")
@Api(tags = "序列生成配置相关接口")
public class ConfigController extends BaseController<ConfigVo, Config> {

    @Autowired
    private ConfigWebMapper currentWebMapper;
    @Autowired
    private IConfigService currentService;

    @Autowired
    private ISequenceGenerator iSequenceGenerator;


     @ApiOperation("添加序列生成配置")
     @PreAuthorize("hasAuthority('config:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public ConfigVo create(@RequestBody @Valid ConfigCreateForm cf) {

         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         Config po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询序列生成配置")
     @PreAuthorize("hasAuthority('config:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public ConfigVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除序列生成配置")
     @PreAuthorize("hasAuthority('config:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新序列生成配置")
     @PreAuthorize("hasAuthority('config:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public ConfigVo update(@PathVariable String id,@RequestBody @Valid ConfigUpdateForm uf) {
         Config po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询序列生成配置")
    @PreAuthorize("hasAuthority('config:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<ConfigVo> listPage(ConfigListPageForm listPageForm) {
         Config po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

    @Override
    public ConfigVo transVo(ConfigVo configVo) {
        configVo.setCurrentValue(iSequenceGenerator.currentSeqValueByConfigId(configVo.getId()));
        return super.transVo(configVo);
    }
}
